<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/22
//      |_|                                         | TpMeCMS

declare(strict_types=1);
namespace ImiApp\ApiServer\Api\Controller\user;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;


/**
 * 签到📸
 * @Auth(name="token",nologin={"no"})
 * @Controller("/user.sign/")
 */
class Sign extends UserBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];


    // /**
    //  * 获取统计积分
    //  *
    //  * @return void
    //  */
    // public function get_count()
    // {
    //     $where['user_id'] = $this->auth->user()->id;
    //     $data['inc'] = Db::name('signlog')->whereEx($where)->where('status','2')->sum('signlog');  
    //     $data['out'] = Db::name('signlog')->whereEx($where)->where('status','1')->sum('signlog');  
    //     $data['this_month_inc'] = Db::name('signlog')->whereEx($where)->whereTime('createtime', 'm')->where('status','2')->sum('signlog');  
    //     $data['this_month_dec'] = Db::name('signlog')->whereEx($where)->whereTime('createtime', 'm')->where('status','1')->sum('signlog');  
    //     return $this->success('统计',$data);
    // }

    /**
     * 每日签到
     *
     * @return void
     * @Action
    */
    public function add_sign()
    {
        $data['user_id'] = $this->auth->user()->id;

        $res = Db::name('signlog')->whereEx($data)->where('date','=', date('Y-m-d'))->find();
        if ($res) {
            return $this->error('今日已签到过' . date('H:i:s', $res['createtime']));
        }
        $day = 1;
        $data['date'] = date('Y-m-d', strtotime("-1 day"));
        $yesterday = Db::name('signlog')->whereEx($data)->find();
        if ($yesterday) {
            if ($yesterday['num'] >= config('sign_day_upgrade') ?: 100) { //签到满100天升级 断掉
                $yesterday['num'] = 0;
            }
            $day = $yesterday['num'] + 1;
        }
        $score = $day * $this->GetEveryDaySignScore();



        $data['date'] = date('Y-m-d');
        $data['createtime'] = time();
        $data['num'] = $day;
        $data['year'] = date('Y');
        $data['m'] = date('n');
        $data['d'] = date('d');
        $data['score'] = $score;

        $id = Db::name('signlog')->insert($data)->getLastInsertId();
        $this->IncUserScore($this->auth->user()->id, $score, "每日签到积分,签到第{$day}天",6,$id);

        //签到升级
        if ($day >= \Imi\Config::get('site.sign_day_upgrade') ?? 100) {
            $this->UpgradeUserSet($this->auth->user()->id, 5, "连续签到第{$day}天,升级"); //status 状态:1=拍OR拼晋级,2=积分晋级,3=团队晋级,5=连续签到晋级
        }

        return $this->success('签到成功');
    }

    /**
     * 获取签到记录列表📸
     *
     * @param string $date 年月(可不传
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     * @Action
    */
    public function get_signlog_list(?int $page = 1,?int  $limit = 10, $date = null)
    {
        $page = $page?:1;
        $limit = $limit?:10;
        if ($date) {
            $date = date('Y-m', strtotime($date));
            $e_date = date('Y-m-d 23:59:59', strtotime($date . " +1 month -1 day"));
            $data['createtime'] = ['between time', [$date, $e_date]];
        }
        $data['user_id'] = $this->auth->user()->id;
        $list = Db::name('signlog')->whereEx($data)->orderRaw('createtime desc')->page($page, $limit)->select()->getArray();
        if ($list)
            foreach ($list as $key => &$val) {
                $this->ChangeFieldData($val);
            }
        return $this->success('ok', $list);
    }

    /**
     * 获取签到记录详情📸
     *
     * @param int $id 签到id
     * @return void
     * @Action
    */
    public function get_signlog_data($id = null)
    {

        $data['id'] = $id;
        $data['user_id'] = $this->auth->user()->id;
        $res = Db::name('signlog')->whereEx($data)->find();
        if (!$res) {
            return $this->error('没有该信息');
        }
        $this->ChangeFieldData($res);
        return $this->success('ok', $res);
    }







    /**
     * 改变字段
     *
     * @param array $val 
     * @return void
     */
    protected function ChangeFieldData(&$val)
    {
        $val['date_str'] = $this->TimeToStr($val['createtime']);
        $val['date'] = date('Y-m-d H:i:s', $val['createtime']);
        return $val;
    }
}
